<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理系统</title>
    <style>
        body {
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
            font-family: Arial, sans-serif;
        }

        .search-form {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }

        input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        #userList {
            list-style: none;
            padding: 0;
        }

        #userList li {
            padding: 12px;
            margin: 8px 0;
            background-color: #f9f9f9;
            border-radius: 4px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .error {
            color: #ff4444;
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <div class="search-form">
        <div>
            <label for="nameInput">用户名：</label>
            <input type="text" id="nameInput" placeholder="请输入用户名">
        </div>

        <div>
            <label for="telInput">电话号码：</label>
            <input type="tel" id="telInput" placeholder="请输入手机号">
        </div>

        <div>
            <label for="snInput">学号：</label>
            <input type="text" id="snInput" placeholder="请输入学号">
        </div>

        <button type="submit" onclick="queryUsers()">查询</button>
    </div>

    <div>
        <ul id="userList"></ul>
        <div id="errorMsg" class="error"></div>
    </div>

    <script>
        function chekLogin() {
            const token = sessionStorage.getItem('token');
            if (!token) {
                window.location.href = './login.html';
            }
        }

        chekLogin();
        async function queryUsers() {
            // 获取输入值并构建查询参数
            const params = new URLSearchParams();
            const name = document.getElementById('nameInput').value.trim();
            const tel = document.getElementById('telInput').value.trim();
            const sn = document.getElementById('snInput').value.trim();

            if (name) params.append('query', `name:${name}`);
            if (tel) params.append('query', `tel:${tel}`);
            if (sn) params.append('query', `sn:${sn}`);

            // 显示加载状态
            document.getElementById('errorMsg').textContent = '加载中...';
            document.getElementById('userList').innerHTML = '';

            try {
                const response = await fetch(`https://study.godoai.com/api/user/?${params.toString()}`, {
                    headers: {
                        Authorization: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NDU4OTQxMDQsImlhdCI6MTc0NTgwNzcwNCwibmFtZSI6IuiUoeS6rOahgiIsInIiOjAsInNuIjoiMjMwMzIwMjAzMDI4In0.DXTy5_xq_uqwBg1YSrtj_Q0caoUjngHppx12QPZY53s"
                    }
                });

                if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);

                const users = await response.json();
                
                if (users.length === 0) {
                    document.getElementById('errorMsg').textContent = '未找到匹配的用户';
                    return;
                }

                renderUserList(users);
                document.getElementById('errorMsg').textContent = '';
                
            } catch (error) {
                console.error('查询出错:', error);
                document.getElementById('errorMsg').textContent = `查询失败: ${error.message}`;
                document.getElementById('userList').innerHTML = '';
            }
        }

        function renderUserList(users) {
            const userList = document.getElementById('userList');
            userList.innerHTML = users.map(user => `
                <li>
                    <strong>${user.Name}</strong><br>
                    学号：${user.Sn}<br>
                    生日：${new Date(user.Birthday).toLocaleDateString()}
                </li>
            `).join('');
        }
    </script>
</body>

</html>